//
//  NoteMainViewController.m
//  SmileRead
//
//  Created by dllo on 15/8/31.
//  Copyright (c) 2015年 JIZ. All rights reserved.
//

#import "NoteMainViewController.h"
#import "Path.h"
#import "FileNameList.h"
#import "NoteAddViewController.h"
#import "NoteTableViewCell.h"
#import "NoteNoTableViewCell.h"
#import "NoteReadViewController.h"
#import "AppDelegate.h"


@interface NoteMainViewController ()<UITableViewDataSource, UITableViewDelegate, UIScrollViewDelegate>

@property (nonatomic, retain) NSMutableArray *arrayOfNote;

@property (nonatomic, retain) UITableView *tableView;

@property (nonatomic, retain) UILabel *mottoLabel;

@property (nonatomic, retain) UIScrollView *backScroll;

@end

@implementation NoteMainViewController

- (instancetype)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        self.arrayOfNote = [NSMutableArray array];
    }
    return self;
}

- (void)dealloc
{
    [_backScroll release];
    [_mottoLabel release];
    [_tableView release];
    [_arrayOfNote release];
    [super dealloc];
}

// 在视图将要显示是取出日记文件中的内容
- (void)viewWillAppear:(BOOL)animated
{
    NSString *path = [Path pathOfLibraryWithFileName:FILE_Note];
    self.arrayOfNote = [NSMutableArray arrayWithContentsOfFile:path];
    if (self.tableView != nil) {
        [Path separatorStyleWithTableview:self.tableView array:self.arrayOfNote];
        [self.tableView reloadData];
    }
    if (self.arrayOfNote.count == 0) {
        [self createAlert];
    }
}

- (void)createAlert
{
    UIAlertController *alert = [UIAlertController alertControllerWithTitle:@"❤️" message:@"快记录您的心情吧" preferredStyle:UIAlertControllerStyleAlert];
    UIAlertAction *action = [UIAlertAction actionWithTitle:@"取消" style:UIAlertActionStyleCancel handler:^(UIAlertAction *action) {
        
    }];
    UIAlertAction *action1 = [UIAlertAction actionWithTitle:@"确定" style:UIAlertActionStyleDefault handler:^(UIAlertAction *action) {
        // 点击确定跳转到添加页面
        NoteAddViewController *add = [[NoteAddViewController alloc] init];
        [self.navigationController pushViewController:add animated:YES];
        [add release];
        
    }];
    [alert addAction:action];
    [alert addAction:action1];
    AppDelegate *tempAppDelegate = (AppDelegate *)[[UIApplication sharedApplication] delegate];
    [tempAppDelegate.window.rootViewController presentViewController:alert animated:YES completion:^{
        
    }];

}

- (void)viewDidLoad {
    [super viewDidLoad];
    self.view.backgroundColor = [UIColor whiteColor];
    self.automaticallyAdjustsScrollViewInsets = NO;
    self.navigationItem.rightBarButtonItem = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemAdd target:self action:@selector(addnote)];
    self.title = @"七日心情❤️";
    [self createSubView];
}

- (void)createSubView
{
  
    self.tableView = [[UITableView alloc] initWithFrame:CGRectMake(0, 64 + 160 * heightFit, self.view.frame.size.width, self.view.frame.size.height - 64 - 160 * heightFit)];
    self.tableView.dataSource = self;
    self.tableView.delegate = self;
    //self.tableView.backgroundColor = [UIColor cyanColor];
    [self.view addSubview:self.tableView];
    [_tableView release];
    
    self.backScroll = [[UIScrollView alloc] initWithFrame:CGRectMake(0 * widthFit, 64, self.view.frame.size.width, 160 * heightFit)];
    self.backScroll.minimumZoomScale = 1;
    self.backScroll.maximumZoomScale = 3;
    self.backScroll.delegate = self;
    [self.view addSubview:self.backScroll];
    
    UIImageView *image =[[UIImageView alloc] initWithFrame:CGRectMake(0, 0, self.view.frame.size.width, 160 * heightFit)];
    image.image = [UIImage imageWithContentsOfFile:[Path pathOfImageWithName:@"sun.png"]];
    [self.backScroll addSubview:image];
    [image release];
    
    UIBlurEffect *blur = [UIBlurEffect effectWithStyle:UIBlurEffectStyleLight];
    UIVisualEffectView *effect = [[UIVisualEffectView alloc] initWithEffect:blur];
    effect.frame = CGRectMake(0, 0, self.view.frame.size.width, 160 * heightFit);
    //effect.alpha = 0.4;
    [self.backScroll addSubview:effect];
    [effect release];
    
    self.mottoLabel = [[UILabel alloc] initWithFrame:CGRectMake(170 * widthFit, 50 * heightFit, self.view.frame.size.width - 170 * widthFit, 100 * heightFit)];
    self.mottoLabel.font = [UIFont systemFontOfSize:17];
    self.mottoLabel.numberOfLines = 0;
    NSAttributedString *string = [[NSAttributedString alloc] initWithString:@"生活的点滴, 积累了你的将来, 当阳光照进你的心房, 你会发现世界很美" attributes:@{NSForegroundColorAttributeName : [UIColor colorWithRed:219 / 255.0 green:216 / 255.0 blue:219 / 255.0 alpha:1]}];
    self.mottoLabel.attributedText = string;
    [effect.contentView addSubview:self.mottoLabel];
}

- (void)scrollViewDidScroll:(UIScrollView *)scrollView
{
    self.backScroll.zoomScale = -(self.tableView.contentOffset.y / 30);
}

- (UIView *)viewForZoomingInScrollView:(UIScrollView *)scrollView
{
    return [self.backScroll.subviews firstObject];
}


- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return self.arrayOfNote.count;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    static NSString *ident = @"cell";
    static NSString *identOfNo = @"no";
    
    NSDictionary *dic = [NSDictionary dictionary];
    if (self.arrayOfNote.count > 0) {
       dic = self.arrayOfNote[indexPath.row];
    }
    
    if ([dic objectForKey:@"image"] == nil) {
        NoteNoTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:identOfNo];
        if (cell == nil) {
            cell = [[[NoteNoTableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:identOfNo] autorelease];
        }
        [cell receiveNote:[dic objectForKey:@"note"] date:[dic objectForKey:@"time"]];
        cell.selectionStyle = UITableViewCellSelectionStyleNone;
        return cell;
    }
    
    else{
        NoteTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:ident];
        if (cell == nil) {
            cell = [[[NoteTableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:ident] autorelease];
        }
        
        [cell receiveImageName:[dic objectForKey:@"image"] date:[dic objectForKey:@"time"] note:[dic objectForKey:@"note"]];
        cell.selectionStyle = UITableViewCellSelectionStyleNone;
        return cell;
    }
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    NSDictionary *dic = self.arrayOfNote[indexPath.row];
    NoteReadViewController *read = [[NoteReadViewController alloc] init];
    [read receiceWithDic:dic];
    [self.navigationController pushViewController:read animated:YES];
    [read release];
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
     NSDictionary *dic = self.arrayOfNote[indexPath.row];
    if ([dic objectForKey:@"image"] == nil) {
        return 120 * heightFit;
    }
    else {
        return 150 * heightFit;
    }
}

- (void)addnote
{
    NoteAddViewController *add = [[NoteAddViewController alloc] init];
    [self.navigationController pushViewController:add animated:YES];
    [add release];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}


@end
